package com.shinaier.laundry.snlstore.offlinecash.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.common.adapter.BaseAdapterNew;
import com.common.adapter.ViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.shinaier.laundry.snlstore.R;
import com.shinaier.laundry.snlstore.network.Constants;
import com.shinaier.laundry.snlstore.network.entity.OfflineOrderDetailEntity;
import com.shinaier.laundry.snlstore.util.CommonTools;

import java.util.List;

/**
 * Created by 张家洛 on 2017/8/1.
 */

public class OfflineOrderDetailAdapter extends BaseAdapterNew<OfflineOrderDetailEntity.OfflineOrderDetailDatas.OfflineOrderDetailItems> {
    private Context context;
    private String type;
    private String isOnline;
    private List<OfflineOrderDetailEntity.OfflineOrderDetailDatas.OfflineOrderDetailItems> mDatas;
    private CheckPhotoListener listener;
    public interface CheckPhotoListener{
        void onClick(int position,int photoPosition);
    }

    public void getPhotoListener(CheckPhotoListener listener){
        this.listener = listener;
    }
    public OfflineOrderDetailAdapter(Context context, List<OfflineOrderDetailEntity.OfflineOrderDetailDatas.OfflineOrderDetailItems> mDatas
        ,String type,String isOnline) {
        super(context, mDatas);
        this.context = context;
        this.type = type;
        this.mDatas = mDatas;
        this.isOnline = isOnline;
    }

    @Override
    public int getCount() {
        if (type != null){
            if (type.equals("1")){  //type 1 的话是个人订单 2 的话是企业订单
                return mDatas.size();
            }else {
                if (mDatas.size() < 5){ //如果企业订单小于5个的话
                    return mDatas.size();
                }else {
                    return 5;
                }
            }
        }else {
            return mDatas.size();
        }

    }

    @Override
    protected int getResourceId(int Position) {
        return R.layout.offline_order_detail_item;
    }

    @Override
    protected void setViewData(View convertView, final int ListviewPosition) {
        OfflineOrderDetailEntity.OfflineOrderDetailDatas.OfflineOrderDetailItems item = getItem(ListviewPosition);
        SimpleDraweeView orderDetailImg = ViewHolder.get(convertView,R.id.order_detail_img);
        TextView tvClothesName = ViewHolder.get(convertView,R.id.tv_clothes_name); //衣服名字
        TextView tvClothesPrice = ViewHolder.get(convertView,R.id.tv_clothes_price); //衣服价格
        TextView clothesNumber = ViewHolder.get(convertView,R.id.clothes_number); //衣物编码
        TextView tvTechnologyPrice = ViewHolder.get(convertView,R.id.tv_technology_price); //特殊工艺加价
        TextView editorMaintainValue = ViewHolder.get(convertView,R.id.editor_maintain_value); //保值金额
        TextView maintainValueCleanNum = ViewHolder.get(convertView,R.id.maintain_value_clean_num); //保值清洗费
        TextView tvTakeClothesTime = ViewHolder.get(convertView,R.id.tv_take_clothes_time); //取衣时间
        TextView tvRemarks = ViewHolder.get(convertView,R.id.tv_remarks); //备注
        GridView orderDetailGv = ViewHolder.get(convertView,R.id.order_detail_gv); //显示图片的GridView
        TextView orderDetailColourInfo = ViewHolder.get(convertView,R.id.order_detail_colour_info); //颜色描述
        TextView questionDetailInfo = ViewHolder.get(convertView,R.id.question_detail_info); //问题描述
        TextView orderCleanedAssess = ViewHolder.get(convertView,R.id.order_cleaned_assess); //洗后预估

        if (item != null){
            if(item.getUrl() != null){
                String imgPath = Constants.Urls.URL_BASE_DOMAIN + item.getUrl();
                Uri uri = Uri.parse(imgPath);
                orderDetailImg.setImageURI(uri);
            }else {
                orderDetailImg.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.not_available_img));
            }

            tvClothesName.setText(item.getgName());
            tvClothesPrice.setText(item.getPrice());
            if (!TextUtils.isEmpty(item.getCleanNumber())){
                clothesNumber.setText("衣物编码:" + item.getCleanNumber());
            }else {
                clothesNumber.setText("衣物编码:******");
            }
            tvTechnologyPrice.setText("特殊工艺加价：￥" + item.getSpecial());

            if (item.getHedging() == 0){ //判断保值金额等于0的处理，不然报错
                maintainValueCleanNum.setText("保值清洗费：￥0.00");
                editorMaintainValue.setText("保值金额：￥0.00");
            }else {
                maintainValueCleanNum.setText("保值清洗费：￥" + CommonTools.formatMoney(item.getHedging()));
                editorMaintainValue.setText("保值金额：￥" + (CommonTools.formatMoney(item.getHedging() * 200)));
            }

            if (isOnline.equals("2")){
                if (!TextUtils.isEmpty(item.getTakeTime())){ //判断取衣时间是否为空
                    tvTakeClothesTime.setVisibility(View.VISIBLE);
                    tvTakeClothesTime.setText(item.getTakeTime());
                }else {
                    tvTakeClothesTime.setVisibility(View.GONE);
                }
            }else {
                tvTakeClothesTime.setVisibility(View.GONE);
            }

            if (item.getSpecialComment() != null){ //判断备注是否为空
                tvRemarks.setVisibility(View.VISIBLE);
                tvRemarks.setText("备注：" + item.getSpecialComment());
            }else {
                tvRemarks.setVisibility(View.GONE);
            }
            if (item.getColor() != null && item.getColorText() != null){ //判断颜色是否为空
                orderDetailColourInfo.setVisibility(View.VISIBLE);
                orderDetailColourInfo.setText(item.getColor() + "," + item.getColorText());
            }else if (item.getColor() != null){
                orderDetailColourInfo.setVisibility(View.VISIBLE);
                orderDetailColourInfo.setText(item.getColor());
            }else if (item.getColorText() != null){
                orderDetailColourInfo.setVisibility(View.VISIBLE);
                orderDetailColourInfo.setText(item.getColorText());
            }else {
                orderDetailColourInfo.setVisibility(View.GONE);
            }
            if (item.getItemNote() != null && item.getNoteText() != null){ //判断问题描述是否为空
                questionDetailInfo.setVisibility(View.VISIBLE);
                questionDetailInfo.setText(item.getItemNote() + "," + item.getNoteText());
            }else if (item.getItemNote() != null){
                questionDetailInfo.setVisibility(View.VISIBLE);
                questionDetailInfo.setText(item.getItemNote());
            }else if (item.getNoteText() != null){
                questionDetailInfo.setVisibility(View.VISIBLE);
                questionDetailInfo.setText(item.getNoteText());
            }else {
                questionDetailInfo.setVisibility(View.GONE);
            }

            if (item.getAssess() != null && item.getAssessText() != null){
                orderCleanedAssess.setVisibility(View.VISIBLE);
                orderCleanedAssess.setText(item.getAssess() + "," + item.getAssessText());
            }else if (item.getAssess() != null){
                orderCleanedAssess.setVisibility(View.VISIBLE);
                orderCleanedAssess.setText(item.getAssess());
            }else if (item.getAssessText() != null){
                orderCleanedAssess.setVisibility(View.VISIBLE);
                orderCleanedAssess.setText(item.getAssessText());
            }else {
                orderCleanedAssess.setVisibility(View.GONE);
            }

            if(item.getImgs() != null && item.getImgs().size() > 0){
                orderDetailGv.setVisibility(View.VISIBLE);
                OfflineOrderDetailGridAdapter offlineOrderDetailGridAdapter = new OfflineOrderDetailGridAdapter(context,item.getImgs());
                orderDetailGv.setAdapter(offlineOrderDetailGridAdapter);
                orderDetailGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if(listener != null){
                            listener.onClick(ListviewPosition,position);
                        }
                    }
                });
            }else {
                orderDetailGv.setVisibility(View.GONE);
            }
        }
    }
}
